﻿<%@ WebHandler Language="C#" Class="GetDownload" %>
using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Tathata.Common.Caching;
using Tathata.Common.DAL;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.EntityCollections;
using Tathata.Common.BLL.Managers;
using Tathata.Common.DAL.Managers;
using Tathata.Common.Utils;
using Tathata.Common;

public class GetDownload : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        if (NopContext.Current.User == null)
            context.Response.Redirect("~/Login.aspx");

        //int orderProductVariantID = CommonHelper.QueryStringInt("OrderProductVariantID");
        //OrderProductVariant orderProductVariant = OrderProductVariantManager.GetByOrderProductVariantID(orderProductVariantID);
        //if (orderProductVariant == null)
        //{
        //    returnError(context, string.Format("Order product variant doesn't exist. Product variant ID={0}", orderProductVariantID));
        //    return;
        //}

      

        //ProductVariant productVariant = orderProductVariant.ProductVariant;
        //if (productVariant == null)
        //{
        //    returnError(context, string.Format("Product variant doesn't exist. ID={0}", orderProductVariant.ProductVariantID));
        //    return;
        //}

        //if (!productVariant.IsDownload)
        //{
        //    returnError(context, string.Format("Product variant is not downloadable. ID={0}", orderProductVariant.ProductVariantID));
        //    return;
        //}

        //Download download = productVariant.Download;
        //if (download == null)
        //{
        //    returnError(context, string.Format("Download is not available any more. ID={0}", productVariant.DownloadID));
        //    return;
        //}

        //context.Response.Clear();
        //context.Response.ContentType = "application/zip";
        //context.Response.AddHeader("Content-disposition", string.Format("filename={0}.zip", orderProductVariant.OrderProductVariantID));

        //using (MemoryStream ms = new MemoryStream(download.DownloadBinary))
        //{
        //    int length;
        //    long dataToRead = download.DownloadBinary.Length;
        //    byte[] buffer = new Byte[10000];

        //    while (dataToRead > 0)
        //    {
        //        if (context.Response.IsClientConnected)
        //        {
        //            length = ms.Read(buffer, 0, 10000);
        //            context.Response.OutputStream.Write(buffer, 0, length);
        //            context.Response.Flush();
        //            buffer = new Byte[10000];
        //            dataToRead = dataToRead - length;
        //        }
        //        else
        //            dataToRead = -1;
        //    }
        //}
    }
    
    private void returnError(HttpContext context, string Message)
    {
        context.Response.Clear();
        context.Response.Write(Message);
        context.Response.Flush(); ;
    }
    
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}